package com.hjs.tools;

import com.hjs.bean.DamageConditionEvaluationStandard;
import com.hjs.bean.DrivingQualityEvaluationStandard;
import com.hjs.bean.OverviewStandard;
import com.hjs.bean.RoadType;
import com.hjs.mapper.StandardMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author ShenXiaojian
 * @date 2020/6/17
 **/
@Slf4j
public class TechnicalConditionEvaluation {
    public static Double calculateRQI(Double IRI) {
        double RQI = 4.98 - 0.34 * IRI;
        if (RQI < 0) {
            return (double) 0;
        } else {
            return RQI;
        }
    }

    public static Double calculatePQI(Double RQI, Double PCI, Integer roadTypeId) {
        double w1 = 0, w2 = 0;
        if (roadTypeId == 1 || roadTypeId == 2) {
            w1 = 0.6;
            w2 = 0.4;
        } else if (roadTypeId == 8 || roadTypeId == 10 || roadTypeId == 11) {
            w1 = 0.4;
            w2 = 0.6;
        }
        return 20 * w1 * RQI + PCI * w2;
    }

    public static Double calculatePCI(Double indiviualScore) {
        double PCI = 100 - indiviualScore;
        if (PCI < 0) {
            return (double) 0;
        } else {
            return PCI;
        }
    }

    public static Integer getRQIGradeIdByRQI(Double RQI, RoadType roadType) {
        Integer grade = null;
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            StandardMapper standardMapper = sqlSession.getMapper(StandardMapper.class);
            List<DrivingQualityEvaluationStandard> allDrivingQualityEvaluationStandard = standardMapper.getAllDrivingQualityEvaluationStandard();
            for(DrivingQualityEvaluationStandard drivingQualityEvaluationStandard : allDrivingQualityEvaluationStandard){
                if (drivingQualityEvaluationStandard.getRoadTypeID().equals(roadType.getRoadTypeID())
                        && drivingQualityEvaluationStandard.getRQIMin()<=RQI
                        && drivingQualityEvaluationStandard.getRQIMax()>=RQI) {
                    grade = drivingQualityEvaluationStandard.getDriverQualityStandardID();
                }
            }
        } catch (Exception e) {
            log.error(e.toString());
            return -1;
        }
        return grade;
    }

    public static Integer getPCIGradeIdByPCI(Double PCI, RoadType roadType) {
        Integer grade = null;
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            StandardMapper standardMapper = sqlSession.getMapper(StandardMapper.class);
            List<DamageConditionEvaluationStandard> allDamageConditionEvaluationStandard = standardMapper.getAllDamageConditionEvaluationStandard();
            for(DamageConditionEvaluationStandard damageConditionEvaluationStandard : allDamageConditionEvaluationStandard){
                if (damageConditionEvaluationStandard.getRoadTypeID().equals(roadType.getRoadTypeID())
                        && damageConditionEvaluationStandard.getPCIMin()<=PCI
                        && damageConditionEvaluationStandard.getPCIMax()>=PCI) {
                    grade = damageConditionEvaluationStandard.getDamageConditionStandardID();
                }
            }
        } catch (Exception e) {
            log.error(e.toString());
            return -1;
        }
        return grade;
    }

    public static Integer getPQIGradeIdByPQI(Double PQI, RoadType roadType) {
        Integer grade = null;
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            StandardMapper standardMapper = sqlSession.getMapper(StandardMapper.class);
            List<OverviewStandard> allOverviewStandard = standardMapper.getAllOverviewStandard();
            for(OverviewStandard overviewStandard : allOverviewStandard){
                if (overviewStandard.getRoadTypeID().equals(roadType.getRoadTypeID())
                        && overviewStandard.getPQIMin()<=PQI
                        && overviewStandard.getPQIMax()>=PQI) {
                    grade = overviewStandard.getOverviewStandardID();
                }
            }
        } catch (Exception e) {
            log.error(e.toString());
            return -1;
        }
        return grade;
    }
}
